package com.tencent.wxcloudrun.service.business.zuke;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tencent.wxcloudrun.config.CustomException;
import com.tencent.wxcloudrun.config.UserContext;
import com.tencent.wxcloudrun.pojo.bo.MediaInfo;
import com.tencent.wxcloudrun.pojo.dto.req.BeZuKeReq;
import com.tencent.wxcloudrun.pojo.dto.resp.GetZuKePageResp;
import com.tencent.wxcloudrun.pojo.po.XPersonInfo;
import com.tencent.wxcloudrun.service.XPersonInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ZuKePersonService {

    @Resource
    private XPersonInfoService xPersonInfoService;

    @Transactional(rollbackFor = Exception.class)
    public String beZuKe(BeZuKeReq req) {
        String openId = UserContext.getOpenId();
        // 查询当前用户的租客信息
        LambdaQueryWrapper<XPersonInfo> xPersonInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        xPersonInfoLambdaQueryWrapper.eq(XPersonInfo::getPersonId, openId);
        xPersonInfoLambdaQueryWrapper.eq(XPersonInfo::getPersonType, "tenant");
        XPersonInfo tenant = xPersonInfoService.getOne(xPersonInfoLambdaQueryWrapper);
        boolean isIncomplete = StrUtil.isBlank(tenant.getCardFront()) || StrUtil.isBlank(tenant.getCardBack()) ||
                StrUtil.isBlank(tenant.getSignPic()) || StrUtil.isBlank(tenant.getCardName()) || StrUtil.isBlank(tenant.getSex()) ||
                StrUtil.isBlank(tenant.getPhone()) || StrUtil.isBlank(tenant.getCardId());
        if (isIncomplete) {
            // 租客信息不完整，需要全量检查
            if (StrUtil.isBlank(req.getCardName())) {
                throw new CustomException("请输入姓名！");
            }
            if (StrUtil.isBlank(req.getSex())) {
                throw new CustomException("请输入性别！");
            }
            if (req.getSignPic() == null || StrUtil.isBlank(req.getSignPic().getFileID())) {
                throw new CustomException("请确保已完成签名！");
            }
            if (req.getCardFront() == null || StrUtil.isBlank(req.getCardFront().getFileID())) {
                throw new CustomException("请上传身份证正面图片！");
            }
            if (req.getCardBack() == null || StrUtil.isBlank(req.getCardBack().getFileID())) {
                throw new CustomException("请上传身份证反面图片！");
            }
            if (StrUtil.isBlank(req.getPhone())) {
                throw new CustomException("手机号为空，请检查！");
            }
            if (StrUtil.isBlank(req.getCardId())) {
                throw new CustomException("身份证号为空，请检查！");
            }
//            if (!IdcardUtil.isValidCard(req.getCardId())) {
//                throw new CustomException("身份证号非法，请检查！");
//            }
//            if (!PhoneUtil.isPhone(req.getPhone())) {
//                throw new CustomException("手机号非法，请检查！");
//            }
        } else {
            // 变更操作，只校验非空字段
            if (StrUtil.isBlank(req.getCardId())) {
                throw new CustomException("身份证号为空，请检查");
            }
            if (StrUtil.isBlank(req.getPhone())) {
                throw new CustomException("手机号为空，请检查");
            }
        }

        XPersonInfo xPersonInfo = new XPersonInfo();
        xPersonInfo.setPersonId(openId);
        if (StrUtil.isNotBlank(req.getPhone())) {
            xPersonInfo.setPhone(req.getPhone());
        }
        if (StrUtil.isNotBlank(req.getCardId())) {
            xPersonInfo.setCardId(req.getCardId());
//            xPersonInfo.setBirthday(IdcardUtil.getBirthDate(req.getCardId()));
        }
        if (StrUtil.isNotBlank(req.getCardName())) {
            xPersonInfo.setCardName(req.getCardName());
        }
        if (StrUtil.isNotBlank(req.getSex())) {
            xPersonInfo.setSex(req.getSex());
        }
        // 仅在租客信息不完整或提供了新值时才更新身份证信息
        if (isIncomplete || (req.getCardFront() != null && StrUtil.isNotBlank(req.getCardFront().getFileID()))) {
            xPersonInfo.setCardFront(MediaInfo.transMediaListToStr(req.getCardFront()));
        }
        if (isIncomplete || (req.getCardBack() != null && StrUtil.isNotBlank(req.getCardBack().getFileID()))) {
            xPersonInfo.setCardBack(MediaInfo.transMediaListToStr(req.getCardBack()));
        }
        if (isIncomplete || (req.getSignPic() != null && StrUtil.isNotBlank(req.getSignPic().getFileID()))) {
            xPersonInfo.setSignPic(MediaInfo.transMediaListToStr(req.getSignPic()));
        }
        xPersonInfo.setUpdateTime(new Date());
        xPersonInfoService.update(xPersonInfo, xPersonInfoLambdaQueryWrapper);
        return "success";
    }


    public GetZuKePageResp getPage() {
        String openId = UserContext.getOpenId();
        LambdaQueryWrapper<XPersonInfo> xPersonInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        xPersonInfoLambdaQueryWrapper.eq(XPersonInfo::getPersonId, openId);
        xPersonInfoLambdaQueryWrapper.eq(XPersonInfo::getPersonType, "tenant");
        XPersonInfo tenant = xPersonInfoService.getOne(xPersonInfoLambdaQueryWrapper);
        GetZuKePageResp getZuKePageResp = new GetZuKePageResp();
        if (StrUtil.isNotBlank(tenant.getCardFront())) {
            getZuKePageResp.setCardFront(JSON.parseObject(tenant.getCardFront(), MediaInfo.class));
        }
        if (StrUtil.isNotBlank(tenant.getCardBack())) {
            getZuKePageResp.setCardBack(JSON.parseObject(tenant.getCardBack(), MediaInfo.class));
        }
        if (StrUtil.isNotBlank(tenant.getSignPic())) {
            getZuKePageResp.setSignPic(JSON.parseObject(tenant.getSignPic(), MediaInfo.class));
        }
        getZuKePageResp.setCardName(tenant.getCardName());
        getZuKePageResp.setSex(tenant.getSex());
        getZuKePageResp.setPhone(tenant.getPhone());
        getZuKePageResp.setCardId(tenant.getCardId());
        return getZuKePageResp;
    }
}
